package me.timlong.Tree;

import java.util.Stack;

public class RootFirstTraverse {

    public void travelFirst(TreeNode root){
        if(null == root)
            return;
        System.out.println(root.val + ", ");
        travelFirst(root.left);
        travelFirst(root.right);
    }

    public void travelFirstNoneRecursion(TreeNode root){
        Stack<TreeNode> stack = new Stack<>();

        if(null == root)
            return;

        TreeNode pointer = root;

        while(null != pointer && !stack.isEmpty()){

            //内层循环走到left为空
            while(null != pointer){
                stack.push(pointer);
                System.out.println(pointer.val);
                pointer = pointer.left;
            }
            //转到右子树
            if(!stack.isEmpty()){
                pointer = stack.pop();
                pointer = pointer.right;
            }
        }


    }




}
